package com.lysj.merchant.management.service;

import cn.hutool.crypto.digest.BCrypt;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.exception.BaseException;
import com.lysj.admin.utils.spring.foundation.base.BaseService;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.admin.utils.web.SelectItem;
import com.lysj.merchant.management.domain.Role;
import com.lysj.merchant.management.domain.User;
import com.lysj.merchant.management.domain.UserRole;
import com.lysj.merchant.management.repository.UserRepository;
import com.lysj.merchant.merchant.domain.Store;
import com.lysj.merchant.merchant.repository.StoreRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

/**
 * @author zk
 * @description 用户表服务层
 * @create 2018-07-25 15:02:19
 **/
@Slf4j
@Service
@Transactional
public class UserService implements BaseService<User> {

    @Resource
    private UserRepository userRepository;

    @Resource
    private RoleService roleService;

    @Resource
    private UserRoleService userRoleService;

    @Override
    public UserRepository getRepository() {
        return userRepository;
    }

    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    /**
     * @author Created by wtl on 2019/3/14 15:54
     * @Description 店长添加员工
     */
    public String add(User entity, String[] roleIds) {
        if (ParamUtil.isBlank(roleIds)) {
            throw new BaseException("请选择角色");
        }
        User user = userRepository.findByPhone(entity.getPhone());
        if (!ParamUtil.isBlank(user)) {
            throw new BaseException("该手机号已存在");
        }
        user = userRepository.findByUsername(entity.getUsername());
        if (!ParamUtil.isBlank(user)) {
            throw new BaseException("该用户已存在");
        }

        entity.setPassword(BCrypt.hashpw("123456"));
        // 员工类型
        int userType = 3;
        for (String s : roleIds) {
            Role role = roleService.findOne(s);
            if (Role.RoleType.MANAGER.getCode().equals(role.getRoleType())) {
                userType = 2;
                break;
            }
        }
        entity.setUserType(userType);
        entity = save(entity);

        for (String s : roleIds) {
            if (!roleService.countRoleId(s)) {
                throw new RuntimeException("角色信息有误");
            }
            UserRole ur = new UserRole();
            ur.setRoleId(s);
            ur.setUserId(entity.getId());
            userRoleService.save(ur);
        }
        return "添加成功";
    }

    /**
     * @author Created by wtl on 2019/3/13 17:05
     * @Description 商户下拉框
     */
    public List<SelectItem> selectItems() {
        return userRepository.selectItem();
    }

    /**
     * @author Created by wtl on 2019/4/12 15:08
     * @Description 修改密码
     */
    public String modifyPassword(String id, String password, String newPassword) {
        if (ParamUtil.isBlank(password)) {
            throw new BaseException("请填写旧密码", Resp.Status.PARAM_ERROR.getCode());
        }
        if (ParamUtil.isBlank(newPassword)) {
            throw new BaseException("请填写新密码", Resp.Status.PARAM_ERROR.getCode());
        }
        User user = userRepository.findOne(id);
        if (user == null) {
            throw new BaseException("查无此用户", Resp.Status.PARAM_ERROR.getCode());
        }
        if (user.getDelFlag().equals(CommonConstant.DEL_FLAG)) {
            throw new BaseException("该用户已被禁用", Resp.Status.PARAM_ERROR.getCode());
        }
        if (!BCrypt.checkpw(password, user.getPassword())) {
            throw new BaseException("密码错误", Resp.Status.PARAM_ERROR.getCode());
        }
        if (!newPassword.matches("^[a-zA-Z0-9_-]{6,20}$")) {
            throw new BaseException("密码应为英文、数字、下划线组成的6-20位字符", Resp.Status.PARAM_ERROR.getCode());
        }
        user.setPassword(BCrypt.hashpw(newPassword));
        userRepository.save(user);
        return "修改成功";
    }
}